/*******************************************************************************
 * Copyright (c) 2006 - 2011 ACIN, Profactor GmbH
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Alois Zoitl, Gerhard Ebenhofer
 *     - initial API and implementation and/or initial documentation
 *******************************************************************************/

#pragma once

#include "forte/basicfb.h"
#include "forte/iec61131_functions.h"
#include "forte/datatypes/forte_array_common.h"
#include "forte/datatypes/forte_array.h"
#include "forte/datatypes/forte_array_fixed.h"
#include "forte/datatypes/forte_array_variable.h"

namespace forte::iec61499::events {
  class FORTE_E_MERGE final : public CBasicFB {
      DECLARE_FIRMWARE_FB(FORTE_E_MERGE)

    private:
      static const TEventID scmEventEI1ID = 0;
      static const TEventID scmEventEI2ID = 1;
      static const TEventID scmEventEOID = 0;

      CIEC_ANY *getVarInternal(size_t) override;

      static const TForteInt16 scmStateSTART = 0;
      static const TForteInt16 scmStateEO = 1;

      void enterStateSTART(CEventChainExecutionThread *const paECET);
      void enterStateEO(CEventChainExecutionThread *const paECET);

      void executeEvent(TEventID paEIID, CEventChainExecutionThread *const paECET) override;

      void readInputData(TEventID paEIID) override;
      void writeOutputData(TEventID paEIID) override;
      void setInitialValues() override;

    public:
      FORTE_E_MERGE(StringId paInstanceNameId, CFBContainer &paContainer);

      CEventConnection conn_EO;

      CIEC_ANY *getDI(size_t) override;
      CIEC_ANY *getDO(size_t) override;
      CEventConnection *getEOConUnchecked(TPortId) override;
      CDataConnection **getDIConUnchecked(TPortId) override;
      CDataConnection *getDOConUnchecked(TPortId) override;

      void evt_EI1() {
        receiveInputEvent(scmEventEI1ID, nullptr);
      }

      void evt_EI2() {
        receiveInputEvent(scmEventEI2ID, nullptr);
      }

      void operator()() {
        evt_EI1();
      }
  };
} // namespace forte::iec61499::events
